<?php

/**
 * 后台文章模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: article.class.php 99 2012-02-16 15:49:31Z zwt007 $
 * http://www.t-mac.org；
 */
class article extends Model
{

    private $db;
    private $class_table;
    private $article_table;
    private $addonarticle_table;
    private $user_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->class_table = DB_PREFIX . 'category';
        $this->article_table = DB_PREFIX . 'article';
        $this->addonarticle_table = DB_PREFIX . 'addonarticle';
        $this->user_table = DB_PREFIX . 'users';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addArticle($table, $info, $mode = 'INSERT', $where = '')
    {
        if ($table == 'article') {
            $table = $this->article_table;
        } else {
            $table = $this->addonarticle_table;
        }
        $rs = $this->db->autoExecute($table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 保存返回article_id
     * @param string $menusMain
     * return Boole
     */
    public function addArticleReturn($info)
    {
        $rs = $this->db->autoInsertReturn($this->article_table, $info);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getArticleInfo($aid)
    {
        $rs = $this->db->getRow("SELECT a.*, b.content FROM $this->article_table a LEFT JOIN $this->addonarticle_table b ON a.article_id = b.article_id WHERE a.article_id = '$aid' LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getClass()
    {
        $rs = $this->db->getAll("SELECT * FROM $this->class_table ORDER BY cat_order DESC, cat_id DESC");
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getArticleList($channelid, $cat_id = null, $search_keyword = null, $url = null)
    {
        $where = " WHERE 1=1";
        if ($url == '')
            $url = PHP_SELF . '?m=article';
        if (!empty($cat_id)) {
            $cat_ids = $this->M('category')->getSonTreeList($cat_id);
            $where .= " AND cat_id IN($cat_ids)";
            $url .= "&cat_id=$cat_id";
        }

        if ($search_keyword != '') {
            $where .= " AND title LIKE '%" . $search_keyword . "%'";
            $url .= "&search_keyword=$search_keyword";
        }

        if (!empty($channelid)) {
            $where .= " AND channel = '$channelid'";
            $url .= "&channelid=$channelid";
        }

        $url .= '&page=';
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table {$where}");
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(20);
        $limit = $pages->getSqlLimit();
        $sql = "SELECT article_id, cat_id, title, time, click_count, channel, status FROM {$this->article_table}{$where} ORDER BY article_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);

        //取所有的资讯栏目 不用LEFT JOIN取class_name
        $rs_class = $this->getClass();
        //取内容模型
        $channeltype = Tmac::config('channel.channeltype');

        //取状态
        $status_array = Tmac::config('article.status');

        $ary_class = array();

        //重组栏目category信息数组
        foreach ($rs_class AS $kk => $vv) {
            $ary_class['cat_name'][$vv['cat_id']] = $vv['cat_name'];
            $ary_class['channeltype'][$vv['cat_id']] = $channeltype[$vv['channeltype']];
        }

        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['cat_name'] = $ary_class['cat_name'][$v['cat_id']];
                $rs[$k]['time'] = date('Y/m/d H:i:s', $v['time']);
                $rs[$k]['channeltype'] = $ary_class['channeltype'][$v['cat_id']];
                $rs[$k]['status'] = $status_array[$v['status']];
            }
        }

        $type = empty($channelid) ? '1' : $channelid;
        //把文章的当前page写到cookies里
        HttpResponse::setCookie('article_page_' . $channelid, $pages->getNowPage());

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!";
        }

        $result = array('rs' => $rs, 'pageCurrent' => $pages->getNowPage(), 'page' => $pages->show(), 'ErrorMsg' => $ErrorMsg);
        return $result;
    }

    /**
     * del
     * @param int $class_id
     */
    public function delArticle($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->article_table WHERE article_id in ($id)");
        $rs = $this->db->execute("DELETE FROM $this->addonarticle_table WHERE article_id in ($id)");
        return $rs;
    }

    /**
     * 取用户Array
     * @param type $uid
     * @return type 
     */
    public function getUserArray($uid)
    {
        if ($uid > 1) {
            $sql = "SELECT uid, nicename FROM $this->user_table WHERE uid = $uid";
        } else {
            $sql = "SELECT uid, nicename FROM $this->user_table ORDER BY uid ASC";
        }
        $rs = $this->db->getAll($sql);
        return $rs;
    }

}